import java.util.HashMap;
import java.util.Map;
// 死锁----让线程获取锁的顺序不一致，导致两个线程都无法获取锁，造成死锁。
//快手一面题

public class DeadLock {
    // 两个HashMap对象，用于模拟两个资源
    private static final HashMap<String, String> map1 = new HashMap<>();
    private static final HashMap<String, String> map2 = new HashMap<>();

    static {
        map1.put("1", "1");
        map2.put("2", "2");
    }

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (map1) {
                System.out.println("Thread1 locked map1");
                try {
                    Thread.sleep(100); // 增加死锁概率
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (map2) {
                    System.out.println("Thread1 locked map2");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (map2) {
                System.out.println("Thread2 locked map2");
                try {
                    Thread.sleep(100); // 增加死锁概率
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (map1) {
                    System.out.println("Thread2 locked map1");
                }
            }
        }).start();
    }
}